$(function () {
    initLayerDate("#startDate",true);
    initLayerDate("#endDate",true);
    initLayerDate("#beginTime",true);
    initLayerDate("#endTime",true);
    initLayerDate("#lookDate",true);
    initLayerDate("#lookToDate",true);
    initLayerDate("#examTime",true);
    initLayerDate("#beginCreateTime",true);
    initLayerDate("#endCreateTime",true);
    selectLearnMap();
    $('body').on('click','.submitOrderList',function () {
        getCourseBuyRecordList(1);
    }).on('click','.submitExperienceList',function () {
        getExperienceRecordList(1);
    }).on('click','.submitIntegralList',function () {
        getIntegralRecordList(1);
    }).on('click','.questionSubmit',function () {
        getQuestionList(1)
    }).on('click','.earningList',function () {
        getEarningList(1)
    }).on('click','.coursePlayRecordSubmit,.learnList',function () {
        getCoursePlayRecordSubmit(1);
    }).on('click', '.lookCourse', function () { //课程详情查看(不含章节)
        var courseId = $(this).data('id');
        var courseName = $(this).data('name');
        var courseTypeKey = $(this).data('type');
        parent.layer.open({
            area: winArea,
            btn: ['关闭'],
            content: baseloaction + "/admin/course/cou/previewCourse?courseId=" + courseId + '&courseType=' + courseTypeKey,
            maxmin: true,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '查看课程[<small>' + courseName + '</small>]',
            type: 2,
            yes: function (index) {
                parent.layer.close(index);
            }
        });
    }).on('click','.examRecordSubmit',function () {
        getExamRecordList(1);
    }).on('click', '.lookPractice', function () {
        var that =$(this);
        //随堂练习得分
        var practice =  that.data('practice');
        //该章节随堂练习总的分
        var catalogPractice =  that.data('catalog-practice');
        //的分通过率
        var scorePercent =  that.data('scorepercent');
        //课程名称
        var courseName =  that.data('course-name');
        //章节名称
        var catalogName =  that.data('catalog-name');
        //计算学员 随堂练习得分率
        var  userPractice =  practice ==0?0:(practice/catalogPractice).toFixed(4)*100;
        //计算学员是否通过随堂练习
        var flag= "否";
         if(userPractice>=scorePercent){
             flag= "是"
         }

      var  html ='<div class="m-t-lg">' +
            '        <dl class="dl-horizontal">' +
            '            <dt>课程名称：</dt>' +
            '            <dd class="m-r"><span class="fs12 c-4e">'+courseName+'</span></dd>' +
            '        </dl>\n' +
            '        <div class="hr-line-dashed"></div>' +
            '' +
            '        <dl class="dl-horizontal">' +
            '            <dt>章节名称：</dt>' +
            '            <dd class="m-r"><span class="fs12 c-4e">'+catalogName+'</span></dd>' +
            '        </dl>\n' +
            '        <div class="hr-line-dashed"></div>' +
            '<dl class="dl-horizontal">' +
            '            <dt>章节随堂练习总分：</dt>' +
            '            <dd class="m-r">' +
            '                <span class="fs12 c-4e">'+catalogPractice+'</span>' +
            '            </dd>\n' +
            '        </dl>\n' +
            '        <div class="hr-line-dashed"></div>' +
            '        <dl class="dl-horizontal">' +
            '            <dt>学员随堂练习得分：</dt>' +
            '            <dd class="m-r">' +
            '                <span class="fs12 c-4e">'+practice+'</span>' +
            '            </dd>' +
            '        </dl>' +
            '        <div class="hr-line-dashed"></div>' +
            '<dl class="dl-horizontal">' +
            '            <dt>课程随堂练习得分率：</dt>' +
            '            <dd class="m-r">' +
            '                <span class="fs12 c-4e">'+scorePercent+'%</span>' +
            '            </dd>' +
            '        </dl>' +
            '        <div class="hr-line-dashed"></div>' +
            '<dl class="dl-horizontal">' +
            '            <dt>学员随堂练习得分率：</dt>' +
            '            <dd class="m-r">' +
            '                <span class="fs12 c-4e">'+userPractice+'%</span>' +
            '            </dd>' +
            '        </dl>' +
            '        <div class="hr-line-dashed"></div>' +
            '<dl class="dl-horizontal">' +
            '            <dt>是否通过：</dt>' +
            '            <dd class="m-r">' +
            '                <span class="fs12 c-4e">'+flag+'</span>' +
            '            </dd>' +
            '        </dl>' +
            '        <div class="hr-line-dashed"></div>' +
            '    </div>'
        parent.layer.open({
            area: winArea,
            btn: ['关闭'],
            content: html,
            maxmin: true,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '查看随堂练习[<small>' + courseName+"-"+catalogName + '</small>]',
            type: 1,
            yes: function (index) {
                parent.layer.close(index);
            }
        });
    }).on('click', '.lookCoursePlayDetail', function () { //课程详情查看(不含章节)
        var courseId = $(this).data('courseid');
        var buyCourseId = $(this).data('buycourseid');
        var userId = $(this).data('userid');
        var catalogId = $(this).data('catalogid');
        var playname = $(this).data('playname');
        parent.layer.open({
            area: winArea,
            btn: ['关闭'],
            content: baseloaction + "/admin/user/cus/queryCoursePlayDetail?userId=" + userId + '&buyCourseId=' + buyCourseId+'&courseId=' + courseId+'&catalogId=' + catalogId,
            maxmin: true,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '查看学习[<small>' + playname + '</small>]明细',
            type: 2,
            yes: function (index) {
                parent.layer.close(index);
            }
        });
    }).on('click','#btn2',function(){
        selectLearnMap();
    }).on('click','.courseExportForm',function(){
        var userId = $("#userId").val();
        exportExecl.init({
                title:"导出学习记录",
                excelUrl: '/admin/user/cus/downloadExcelDetailsList?userId=' + userId,
                type:6
            }
        )
    }).on('click', '.queryRecord', function () {
        getQustionRecord();
    }).on('click','.courseCount',function () {
        getCourseCount(1);
    });


});

/**
 * 获取订单列表页面
 * @param pageNo 当前页码
 */
function getCourseBuyRecordList(pageNo) {
    var params = $("#userIdForm").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/ajax/queryCourseBuyRecord',//请求URL
        contextId:'order_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getCourseBuyRecordList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'11', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj)
}

/**
 * 获取角色列表页面
 * @param pageNo 当前页码
 */
function getIntegralRecordList(pageNo) {
    var params = $("#integralRecordForm").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/ajax/queryIntegralRecord',//请求URL
        contextId:'integral_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getIntegralRecordList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'select_all_integral', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj)
}

/**
 * 获取经验记录
 */
function getExperienceRecordList(pageNo) {
    var params = $("#userIdForm").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/ajax/queryAjaxUserExpRecordList',//请求URL
        contextId:'experience_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getExperienceRecordList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'11', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj);
}

/**
 * 获取提问记录
 */
function getQuestionList(pageNo) {
    var params = $("#userIdForm").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/ajax/queryQuestionByUserId',//请求URL
        contextId:'question_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getQuestionList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'11', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj);
}

/**
 * 获取收益记录
 */
function getEarningList(pageNo) {
    var params = $("#earning-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/ajax/getEarningList',//请求URL
        contextId:'earning_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getEarningList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'11', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj);
}

/**
 * 获取课程播放
 * @param pageNo
 */
function getCoursePlayRecordSubmit(pageNo) {
    var params = $("#play-record-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/queryCoursePlayRecord',//请求URL
        contextId:'coursePlayRecord_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getCoursePlayRecordSubmit, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'11', //全选框ID
        callback:function () {//数据加载完成回调；
        }
    };
    ajaxGetListData(obj)
}

/**
 * 获取考试记录
 * @param pageNo
 */
function getExamRecordList(pageNo) {
    var params = $("#exam-record-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/queryExamRecord',//请求URL
        contextId:'exam_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getExamRecordList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'select_all_integral', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj);
}

/**
 * 获取用户今日学习时长
 */
function selectOneDayTime() {
    var userId = $("#userId").val();

    $.ajax({
        url:  baseloaction + "/admin/user/cus/selectOneDayTime?userId="+userId,
        type: "get",
        dataType: "json",
        contentType: 'application/json; charset=UTF-8',
        success: function (result) {
            $("#learnToDayTime").text("今日学习时长："+((result/60/60).toFixed(2))+"小时")
        },
        error: function (error) {
            icModale(0, '系统繁忙，请稍后再试');
        }
    });
}

function getQustionRecord() {
    var url = baseloaction + '/admin/user/cus/queryQuestionRecord';//请求URL
    $.ajax({
        url: url,
        dataType: "text",
        data: {"userId": userId},
        success: function (res) {
            $("#query_record").html(res)
        }
    })

}

/**
 * 获取课程学习进度
 * @param pageNo 当前页码
 */
function getCourseCount(pageNo) {
    var params = $("#userIdForm").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/user/cus/queryCourseCount',//请求URL
        contextId:'course_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getCourseCount, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'11', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj)
}
/**
 * 生成学习记录折线图
 */
function selectLearnMap() {
    var userId = $("#userId").val();
    var params = $("#search-form").serializeJSON();
    params.userId=userId;
    params = JSON.stringify(params);
    $.ajax({
        url:  baseloaction + "/admin/user/cus/selectLearnTimeMap",
        type: "post",
        data: params,
        dataType: "json",
        contentType: 'application/json; charset=UTF-8',
        success: function (result) {
            var valueArray =[];
            var keyArray =[];
            if(result.length>0){
                result.forEach(function(value,index){
                    $.map(value,function(value,key){

                        if(key=='time'){
                            keyArray.push(value);
                        }else{
                            valueArray.push((value/60/60).toFixed(2));
                        }

                    })
                });
            }else{
                var content ='';
                var beginCreateTime = $("#beginCreateTime").val();
                var endCreateTime = $("#endCreateTime").val();
                if(beginCreateTime!=null&&(endCreateTime==null||endCreateTime==""&&beginCreateTime!="")){
                    content ="之后没有学习记录"
                }
                if(endCreateTime!=null&&(beginCreateTime==null||beginCreateTime=="")&&endCreateTime!=""){
                    content ="之前没有学习记录"
                }
                if(beginCreateTime!=null && beginCreateTime!="" && endCreateTime!=null && endCreateTime!=""){
                    content =   beginCreateTime+'_'+endCreateTime+"的范围内没有用户的学习记录"
                }
                if(!content == ''){
                    icModale(0, content);
                }

            }
            learnBrokenLine(valueArray,keyArray)

        },
        error: function (error) {
            console.dir(error)
            parent.layer.closeAll();
            icModale(0, '系统繁忙，请稍后再试');
        }
    });
}

function learnBrokenLine(valueArray,keyArray){
    var myChart = echarts.init(document.getElementById('container'));
    var colors = ['#62A1FE', '#4ECE39', '#675bba'];
    // 指定图表的配置项和数据
    var   option = {
        color: colors,

        tooltip : {
            trigger: 'axis',
            axisPointer: {
                animation: false
            }
        },
        //x轴时间轴
        xAxis : [
            {
                type : 'category',
                data :keyArray
                ,textStyle: {
                    color: '#fff'
                }
            }
        ],
        yAxis: [
            {
                name: '时长(小时/h)',
                type: 'value',textStyle: {
                    color: '#fff'
                }
            }
        ],
        //y轴
        series: [
            {
                name:'时间',
                type:'line',
                animation: false,
                data:valueArray
            }
        ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option,true);
    //自适应
    window.onresize = myChart.resize;
}